package com.usoft4;

import akka.actor.ReceiveTimeout;
import akka.actor.UntypedActor;
import scala.concurrent.duration.Duration;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class MyActor extends UntypedActor {
    private int x;
    private int y;

    public MyActor(int x, int y) {
        this.x = x;
        this.y = y;
        // To set an initial delay
        this.getContext().setReceiveTimeout(Duration.create("10 seconds"));
    }

    @Override
    public void onReceive(Object message) throws Exception {
        if (message.equals("Hello")) {
            // To set in a response to a message
            getContext().setReceiveTimeout(Duration.create("1 second"));
        } else if (message instanceof ReceiveTimeout) {
            System.out.println("接收消息超时");
            // To turn it off
            getContext().setReceiveTimeout(Duration.Undefined());
        } else {
            unhandled(message);
        }
    }
}
